package com.ruoyi.api.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.api.annotation.Login;
import com.ruoyi.business.app.vo.AppCarWashOrderVo;
import com.ruoyi.business.app.vo.AppCarWashVo;
import com.ruoyi.business.app.vo.AppPurchaseVo;
import com.ruoyi.business.app.vo.AppUserCouponVo;
import com.ruoyi.business.domain.HcxCarWash;
import com.ruoyi.business.domain.HcxMerchantOrder;
import com.ruoyi.business.service.IHcxCarWashService;
import com.ruoyi.business.service.IHcxMerchantOrderService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 用户洗车卡订单相关接口 购买完洗车卡就是会员
 * @author: kubilewang
 * @date: 2022/8/23 9:41
 * @param:
 * @Return:
 **/
@Api(tags = "我的--->洗车卡")
@AllArgsConstructor
@RestController
@RequestMapping("/api/carWash")
public class MyVipController extends BaseController {

    private final IHcxCarWashService iHcxCarWashService;

    private final IHcxMerchantOrderService hcxMerchantOrderService;

    @Login
    @ApiOperation("查询洗车卡信息")
    @GetMapping("/detail")
    public AjaxResult<AppCarWashVo> detail() {
        List<AppCarWashVo> appCarWashVos = iHcxCarWashService.listVo(new LambdaQueryWrapper<HcxCarWash>(), AppCarWashVo.class);
        AssertUtils.isTrue(CollectionUtil.isNotEmpty(appCarWashVos), "请现在后台添加洗车卡!");
        return AjaxResult.success(appCarWashVos.get(0));
    }


    @Login
    @ApiOperation("查询用户拥有所拥有的优惠卷")
    @GetMapping("/coupon")
    public AjaxResult<AppUserCouponVo> coupon(@ApiParam(value = "洗车卡id", required = true) @RequestParam Long carWashId) {
        AppUserCouponVo appUserCouponVo = iHcxCarWashService.queryUserCoupon(carWashId);
        return AjaxResult.success(appUserCouponVo);
    }


    @Login
    @ApiOperation("洗车卡购买下单接口")
    @RepeatSubmit
    @PostMapping("/purchase")
    public AjaxResult<AppPurchaseVo> purchase(HttpServletRequest request, @ApiParam(value = "洗车卡id", required = true) @RequestParam Long carWashId,
                                              @ApiParam(value = "优惠券id") @RequestParam(required = false) Long couponId,
                                              @ApiParam(value = "支付方式(1 微信支付 2支付宝支付 3银联支付 4余额支付)", required = true) @RequestParam String payType
    ) throws Exception {
        AppPurchaseVo purchase = iHcxCarWashService.purchase(request, carWashId, couponId, payType);
        return AjaxResult.success(purchase);
    }


    /**
     * @description: 优先展示最早购买的那张(优先核销最早购买的那张)
     * @author: kubilewang
     * @date: 2022/9/9 16:32
     * @param:
     * @Return: AjaxResult<AppCarWashVo>
     **/
    @Login
    @ApiOperation("我购买的洗车卡信息")
    @GetMapping("/myVip")
    public AjaxResult<AppCarWashOrderVo> myVip() {
        List<AppCarWashOrderVo> appCarWashOrderVos = hcxMerchantOrderService.listVo(new LambdaQueryWrapper<HcxMerchantOrder>()
                        .eq(HcxMerchantOrder::getUserId, SecurityUtils.getUserId())
                        .eq(HcxMerchantOrder::getOrderType, Constants.ONE)
                        .orderByDesc(HcxMerchantOrder::getCreateTime),
                AppCarWashOrderVo.class);
        return AjaxResult.success(appCarWashOrderVos.get(0));
    }

}
